package com.lsa.letcode.the28.strstr;

import org.junit.Assert;
import org.junit.Test;

public class StrStrTest {
	@Test
	public void test() {
		Solution s = new Solution();
		
		test(s, "abcdefgh", "e");
		test(s, "abcdexfgh", "ex");
		test(s, "abcdexyfgh", "exy");
		test(s, "aaab", "aaa");
		test(s, "baaa", "aaa");
		test(s, "baaac", "aaa");
		test(s, "baaacaaad", "aaa");
		test(s, "baaacaaaaxd", "aaaax");
		test(s, "abcdefabcdxyz", "abcdef");
		test(s, "abcdefabcdxyz", "abcdx");
		test(s, "abcdefabcdyyzabcdx", "abcdx");
		test(s, "abcdxabcdefabcdyyzabcdz", "abcdx");
		
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < 100000; i++) {
			for (int j = 0; j < 3; j++) {
				sb.append((char)('a'+j));
			}
		}
		StringBuilder sbpat = new StringBuilder();
		for (int j = 0; j < 4; j++) {
			sbpat.append((char)('a'+j));
		}
		sb.append(sbpat.toString());
		
//		StringBuilder sb = new StringBuilder();
//		for (int i = 1; i < 1000; i++) {
//			String str = StringUtils.rightPad("", i, (char)('a' + (i % 26)));
//			sb.append(str);
//		}
//		
		long start = System.currentTimeMillis();
		String sbStr = sb.toString();
		test(s, sbStr, sbpat.toString());
		for (int i = 0; i < 1000; i++) {
//			int random = Utils.random(1, 999);
//			String str = StringUtils.rightPad("", random, (char)('a' + (random % 26)));
//			sbStr.indexOf(sbpat.toString());
			s.strStr(sbStr, sbpat.toString());
		}
		System.out.println("Took :" + (System.currentTimeMillis() - start));
	}

	private void test(Solution s, String str, String subStr) {
		Assert.assertEquals(str.indexOf(subStr), s.strStr(str, subStr));
	}
}
